package com.chenshang.service.intl;

import java.util.Map;

import com.chenshang.util.JaxbUtil;
import com.travelzen.flight.base.TzRequest;
import com.travelzen.flight.base.TzResponse;
import com.travelzen.flight.international.FlightSearchRequest;
import com.travelzen.flight.international.FlightSearchResponse;
import com.travelzen.flight.international.FlightSegmentResult;
import com.travelzen.flight.international.FsFlightSegment;
import com.travelzen.flight.international.InventoryQueryRequest;
import com.travelzen.flight.international.IqFlightScheduled;

public class CreatCabinCheckRequest implements Runnable {
	String request, response;
	Map<String, String> result;

	@Override
	public void run() {
		long start = System.currentTimeMillis();
		TzRequest tzRequest = JaxbUtil.convertToTzRequestByXml(request);
		FlightSearchRequest flightSearchRequest = (FlightSearchRequest) tzRequest.getRequestEntity();
		TzResponse tzResponse = JaxbUtil.convertToTzResponseByXml(response);
		FlightSearchResponse flightSearchResponse = (FlightSearchResponse) tzResponse.getResponseEntity();
		StringBuffer inventoryQueryReq = new StringBuffer();
		for (FlightSegmentResult flightSegmentResult : flightSearchResponse.getFlightSegmentResult()) {

			InventoryQueryRequest inventoryQueryRequest = new InventoryQueryRequest();

			inventoryQueryRequest.setFreightLimitQueryID(flightSegmentResult.getFreightRuleQueryID());

			for (FsFlightSegment fsFlightSegmen : flightSegmentResult.getSegmentList()) {
				for (com.travelzen.flight.international.FsFlightScheduled fsFlightScheduled : fsFlightSegmen.getFlightScheduled()) {
					IqFlightScheduled iqFlightScheduled = new IqFlightScheduled();
					iqFlightScheduled.setBookPersonCount(1);
					iqFlightScheduled.setCabinCode(fsFlightSegmen.getCabinCode());
					iqFlightScheduled.setFlightNo(fsFlightScheduled.getFlightNo());
					iqFlightScheduled.setFromAirport(fsFlightScheduled.getFromAirport());
					iqFlightScheduled.setFromDate(fsFlightScheduled.getFromDate());
					iqFlightScheduled.setToAirport(fsFlightScheduled.getToAirport());
					inventoryQueryRequest.getFlightSchedule().add(iqFlightScheduled);
				}
			}
			tzRequest.setRequestEntity(inventoryQueryRequest);

			inventoryQueryReq.append(JaxbUtil.convertToXml(tzRequest)).append("\n");
		}
		result.put("cabinCheckRequest", inventoryQueryReq.toString());
		System.out.println("1用时：" + (System.currentTimeMillis() - start));
	}

	public Map<String, String> getResult() {
		return result;
	}

	public void setResult(Map<String, String> result) {
		this.result = result;
	}

	public String getRequest() {
		return request;
	}

	public void setRequest(String request) {
		this.request = request;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

}
